<?php
class ProjectController extends Zend_Controller_Action
{
	private $_config;
    public function init ()
    {
        $this->_config = new Zend_Config_Ini(CONFIG_PATH . '/rolesDefault.ini',APPLICATION_ENV);
        
        $acl = new My_Acl();
        $auth = $acl->checkAllowed($this->_request);
        
    }

    public function indexAction ()
    {
        $model_project = new Default_Model_Project();
        $this->view->project_list = $model_project->getAllData();
    }

    public function deleteAction ()
    {
        $model_project = new Default_Model_Project();
        $id = (int)($this->_getParam('id', 0));
        $checkProject = $model_project->checkProject($id)->toArray();
        if($checkProject){
            $this->view->mess = 'Dự án này vẫn còn thiết bị. Vui lòng chuyển thiết bị qua dự án khác!!!';
        }else{
            if($this->getRequest()->isPost()){
                $del = $this->getRequest()->getPost('del');
                if ($del == 'Có') {
                    $projectId = $this->getRequest()->getPost('projectId');
                    $model_project->deleteData($projectId);
                }
                $this->_helper->redirector('index');
            }else{
                $this->view->project = $model_project->getDataById($id);
            }
        }
    }

    public function addAction ()
    {
        $form = new Default_Form_Project();
        $form->submit->setLabel('Thêm mới!');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData)){
                
                $projectName = $form->getValue('projectName');
                $note = $form->getValue('note');
                
                $auth = Zend_Auth::getInstance();
                $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB'));
                $userInfo = $auth->getIdentity();
                $model_users = new Default_Model_Users();
                $users_check = $model_users->getUserByUsername($userInfo['username']);
                
                $model_project = new Default_Model_Project();
                $idProject = $model_project->addData($projectName, $note, $users_check->userId);
                
                /*
				//lay resouceId trong config
				$resourceId = $this->_config->rolesDefault->resourceId->view;

				//lay danh sách quyền từ config
                $admin = $this->_config->rolesDefault->admin->resources;
                $manager = $this->_config->rolesDefault->manager->resources;
                $leader = $this->_config->rolesDefault->leader->resources;
                $engineer = $this->_config->rolesDefault->engineer->resources;

				//lay danh sach các quyền view trong config
				$adminView = $this->_config->rolesDefault->admin->view;
                $managerView = $this->_config->rolesDefault->manager->view;
                $leaderView = $this->_config->rolesDefault->leader->view;
                $engineerView = $this->_config->rolesDefault->engineer->view;
                
        		//insert 3 roles mới
                $model_roles = new Default_Model_Roles();
                $idRole[] = $model_roles->addData('leader '.$projectName);
                $idRole[] = $model_roles->addData('engineer '.$projectName);
                $idRole[] = $model_roles->addData('manager '.$projectName);

        		//tao array role admin
        		$count = count($admin);
        		$dataPrivileges = array();
        		for($i=0;$i<$count;++$i){
        			$dataPrivileges[] = '('.'1'. ',' .$idProject . ',' .$admin->$i. ','. 'NULL' .')';
        		}
        		//tao array role leader
        		$count = count($leader);
        		for($i=0;$i<$count;++$i){
        			if($leader->$i == '1' || $leader->$i == '2'){
        				$countView = count($leaderView);
        				$array = array();
        				for($j=0;$j<$countView;++$j){
        					$array[] = $leaderView->$j;
        				}
        				$view = array();
        				$view = json_encode($array);
        				$dataPrivileges[] = '('.$idRole[0] . ',' .$idProject . ',' .$leader->$i. ',\''. $view.'\')';
        
        			}else{
        				$dataPrivileges[] = '('.$idRole[0] . ',' .$idProject . ',' .$leader->$i. ',' . 'NULL' . ')';
        			}
        		}
        
        		//tao array role engineer
        		$count = count($engineer);
        		for($i=0;$i<$count;++$i){
        			if($engineer->$i == '1' || $engineer->$i == '2'){
        				$countView = count($engineerView);
        				$array = array();
        				for($j=0;$j<$countView;++$j){
        					$array[] = $engineerView->$j;
        				}
        				$view = array();
        				$view = json_encode($array);
        				$dataPrivileges[] = '('.$idRole[1] . ',' .$idProject . ',' .$engineer->$i. ',\''. $view.'\')';
        			}else{
        				$dataPrivileges[] = '('.$idRole[1] . ',' .$idProject . ',' .$engineer->$i. ',' . 'NULL' . ')';
        			}
        		}
        
        		//tao array role manager
        		$count = count($manager);
        
        		for($i=0;$i<$count;++$i){
        			if($manager->$i == '1' || $manager->$i == '2'){
        				$countView = count($managerView);
        				$array = array();
        				for($j=0;$j<$countView;++$j){
        					$array[] = $managerView->$j;
        				}
        				$view = array();
        				$view = json_encode($array);
        
        				$dataPrivileges[] = '('.$idRole[2] . ',' .$idProject . ',' .$manager->$i. ',\''. $view.'\')';
        			}else{
        				$dataPrivileges[] = '('.$idRole[2] . ',' .$idProject . ',' .$manager->$i. ',' . 'NULL' . ')';
        			}
        		}
                
        		//insert privileges cho các role của dự án
                $model_privilege = new Default_Model_Privilege();
                $model_privilege->addMultiData($dataPrivileges);
                */
                $this->_helper->redirector('index','project');
            }else{
                $form->populate($formData);
            }
        }
    }

    public function editAction()
    {
		$form = new Default_Form_Project();
		$form->submit->setLabel('Lưu');
		$this->view->form = $form;

		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$projectId = (int) $form->getValue('projectId');
				$projectName = $form->getValue('projectName');
				$note = $form->getValue('note');
                
				$model_project = new Default_Model_Project();
				$model_project->updateData($projectId, $projectName, $note);

				$this->_helper->redirector('index','project');
			}else{
				$form->populate($formData);
			}
		}else{
			$id = (int)($this->_getParam('id', 0));
            
			$model_project = new Default_Model_Project();
            $project_check = $model_project->getDataById($id);
			$form->populate($project_check->toArray());
		}
    }
}





